package com.beagle.njddapp.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.bean.owner.NjBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author hxx
 * @date 2021-03-19 18:46
 * 消息adapter
 */
public class NjManagerListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<NjBean> njBeanList;
    private Context context;
    private NjListItemClickLisenter<NjBean> onItemClickLisenter;

    public void setOnItemClickLisenter(NjListItemClickLisenter<NjBean> onItemClickLisenter) {
        this.onItemClickLisenter = onItemClickLisenter;
    }

    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            NjBean njBean= (NjBean) view.getTag();
            if (njBean==null){
                return;
            }
            if (view.getId()==R.id.iv_delete){
                onItemClickLisenter.delete(njBean);
            }else {
                onItemClickLisenter.onItemClickLisenter(njBean);
            }
        }
    };

    public NjManagerListAdapter(List<NjBean> njBeanList, Context context) {
        this.njBeanList = njBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_device_list,null);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyHolder myHolder= (MyHolder) holder;
        NjBean njBean=njBeanList.get(position);
        myHolder.tvName.setText(njBean.getMeshNum());
        myHolder.tvOwner.setText(njBean.getMeshUserName());
        myHolder.tvType.setText(njBean.getTypeName());
        myHolder.tvNumber.setText(njBean.getSerialNo());
        myHolder.itemView.setTag(njBean);
        myHolder.tvTime.setText(njBean.getProductionDate());
        myHolder.ivdelete.setTag(njBean);
        myHolder.ivdelete.setOnClickListener(onClickListener);
        myHolder.itemView.setOnClickListener(onClickListener);
        myHolder.setIsRecyclable(false);
        Glide.with(context).load(njBean.getFrontPictureUrl()).into(myHolder.ivDevice);

    }

    @Override
    public int getItemCount() {
        return njBeanList.size();
    }
    public class MyHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_device_name)
        TextView tvName;
        @BindView(R.id.tv_device_number)
        TextView tvNumber;
        @BindView(R.id.tv_device_type)
        TextView tvType;
        @BindView(R.id.tv_device_owner)
        TextView tvOwner;
        @BindView(R.id.tv_device_time)
        TextView tvTime;
        @BindView(R.id.iv_device)
        ImageView ivDevice;
        @BindView(R.id.iv_delete)
        ImageView ivdelete;


        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
    public interface  NjListItemClickLisenter<T> extends  RecyclerViewItemClickLisenter{
        void delete(T t);

    }

}
